//
//  RechargeViewController.m
//  dangdang
//
//  Created by air on 16/1/27.
//  Copyright © 2016年 luweijie. All rights reserved.
//

#import "RechargeViewController.h"
#import "BalanceViewController.h"

@interface RechargeViewController ()<UITextFieldDelegate>

@end

@implementation RechargeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self buttonLayout];
    
    _bottomTextView.text = @"温馨提示：\n1: 充值到账时间5~10分钟，充值30分钟后仍未到账请联系客服。\n\n2: 客服热线：400-8888-000";
    _bottomTextView.userInteractionEnabled = NO;
    _rechargeTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    _rechargeTextField.delegate = self;
    
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fingerTapped:)];
    
    [self.view addGestureRecognizer:singleTap];
    
    self.title = @"充值";
}

-(void)fingerTapped:(UITapGestureRecognizer *)gestureRecognizer

{
    [self.view endEditing:YES];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    [_rechargeTextField resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//    return [self validateNumber:string];
    //控制长度
    if(range.location>=20)
        return NO;
    
    const char * ch=[string cStringUsingEncoding:NSUTF8StringEncoding];
    if(*ch == 0)
        return YES;
    //字符0－9 和 .
    if( *ch != 46 && ( *ch<48 || *ch>57) )
        return NO;
    
    //有了小数点
    if([textField.text rangeOfString:@"."].length==1)
    {
        NSUInteger length=[textField.text rangeOfString:@"."].location;
        
        //小数点后面两位小数 且不能再是小数点
        if([[textField.text substringFromIndex:length] length]>3 || *ch ==46)   //3表示后面小数位的个数。。
            
            return NO;
    }
    
    return YES;
}

- (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}

- (void)buttonLayout{
    _backView.layer.cornerRadius = 8;
    _backView.layer.borderWidth = 1;
    _backView.layer.borderColor = [[[UIColor lightGrayColor] colorWithAlphaComponent:0.5] CGColor];
    _nextStepButton.layer.cornerRadius = 8;
}

- (IBAction)nextStepBtn:(UIButton *)sender {
    if ([_rechargeTextField.text isEqualToString:@""] || _rechargeTextField.text == nil) {
        [self alertViewShowWithAlertString:@"请输入充值金额"];
    }else{
        BalanceViewController * balanceboardVC = [[UIStoryboard storyboardWithName:@"BalanceViewController" bundle:nil] instantiateViewControllerWithIdentifier:@"BalanceViewController"];
        balanceboardVC.rechargeViewController = self;
        [self.navigationController pushViewController:balanceboardVC animated:YES];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
